package com.application.model.universidades.universidadesorg.retrieve;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.application.model.RetrivierWeb;
import com.application.model.universidades.universidadesorg.manager.ManagerUniversidadesOrg;

public class ManagerBusqueda extends RetrivierWeb{

	private ManagerDetalle				managerDetalle;
//	private ManagerUniversidadesOrg 	informacion;
	
	public ManagerBusqueda(ManagerUniversidadesOrg informacion) {
//		this.informacion = informacion;
		managerDetalle = new ManagerDetalle(informacion);
	}
	

	@Override
	public void processContent(String data, String parentName) {
//		if(parentName.equals("busqueda")){
        processContentBusqueda(data,parentName);
//        }else if(parentName.equals("detalle")){
////        	managerDetalle.processContentDetalle(data,parentName);
//        }
	}
	public void processContentBusqueda(String data, String parentName) {
    	Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        String		provincia = "";
        String		informacion = "";
    	//infoWindowAnchor = new GPoint(.*?)GUnload()
//        data = unescapeHtml(data);
        //data = StringEscapeUtils.escapeSql(data);
    	regex = "<b>(.*?)</b>(.*?)</a><br><[br>|/font>]";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
        	provincia = getProvincia(matcher.group(1));
        	informacion = matcher.group(2);
        	logger.info("provincia "+provincia);
        	if(!provincia.equalsIgnoreCase("Colombia")){
        		procesarAvisos(provincia,informacion);
        	}
        }
    }
	
	private String getProvincia(String data) {
		String value = "";
		if(!data.isEmpty()){
			value = data.replace(".", "");
		}
		return value;
	}


	private void procesarAvisos(String provincia, String data) {
    	Pattern     pattern = null;
        Matcher     matcher = null;
        String      regex = null;
        
        
        String      urlDetalle = "";
        
        
        regex = "<a href=\"(.*?)\">";
        pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.MULTILINE|Pattern.DOTALL|Pattern.UNICODE_CASE|Pattern.UNIX_LINES);
        matcher = pattern.matcher(data);
        
        while (matcher.find()) {
            urlDetalle = matcher.group(1);            
            managerDetalle.retrivie(urlDetalle, provincia, null, null, "iso-8859-1");
        }
		
	}
	
	
	@Override
	public void retrieve() {		
	}
	
}
